import { GameComponent } from "@sapi-game/main";
import { EffectIds } from "@sapi-game/utils/vanila-data";
import { HideAndSeekMainState } from "../mainState";
import { HideAndSeekRole } from "../player";
import { HideAndSeekRespawn } from "../components/respawn";
import { SeekerEquipment } from "../components/seekerEquip";
import { GameMode } from "@minecraft/server";

export class HideAndSeekClassic extends GameComponent<HideAndSeekMainState> {
    override onAttach(): void {
        this.state.drawSeeker();
        this.startTitle(); //标题显示
        this.giveEffect(); //给效果
        const equipComp = this.state.getComponent(SeekerEquipment);
        //延迟开始
        this.runner.runDelay(() => {
            equipComp.loadEquipForAll(); //给装备
            this.state.start();
            this.subRespawn();
        }, 120);
    }

    subRespawn() {
        //订阅死亡事件
        this.state.getComponent(HideAndSeekRespawn).sub((p, type) => {
            if (type == "catch") {
                p.alive = false;
                p.player?.setGameMode(GameMode.Spectator);
                this.context.players.sendMessage(
                    `§c${p.name} §l的烤肠被抢走了`
                );
            }
        });
    }

    giveEffect() {
        this.context.players.forEach((p) => {
            if (p.role == HideAndSeekRole.Hider) {
                p.player.addEffect(EffectIds.Invisibility, 200, {
                    showParticles: false,
                });
                p.player.addEffect(EffectIds.Speed, 100, {
                    showParticles: false,
                });
            }
            //抓捕者
            if (p.role == HideAndSeekRole.Seeker) {
                p.player.addEffect(EffectIds.Blindness, 100, {
                    showParticles: false,
                });
                p.player.addEffect(EffectIds.Slowness, 100, {
                    showParticles: false,
                });
            }
        });
    }

    startTitle() {
        this.context.players.forEach((p) => {
            if (p.role == HideAndSeekRole.Hider) {
                p.title("§c游戏开始", "§b你是躲藏者，努力活到最后！");
            } else if (p.role == HideAndSeekRole.Seeker) {
                p.title("§c游戏开始", "§b你是追捕者，快去把躲藏者抓光光！");
            }
        });
    }
}
